﻿using HtmlAgilityPack;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WebNotice
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            InitializeDataDirectory();
        }
        private void InitializeDataDirectory()
        {
            string dataDir = Path.Combine(Application.StartupPath, "data");
            if (!Directory.Exists(dataDir))
            {
                Directory.CreateDirectory(dataDir);
            }
        }

        private void btnFetch_Click(object sender, EventArgs e)
        {
            string[] websties = new string[]
            {
                "https://www.huizhou.gov.cn/zwgk/rsxx/sydwzp",
                "https://www.huidong.gov.cn/hdxrlzyhshbzj/xrlzyhshbzj/zpgg/",
                "https://www.huiyang.gov.cn/zwgk/hydt/tzgg/index.html",
                "http://www.dayawan.gov.cn/gzdt/tzgg/"
            };

            Web1(dataGridView1, websties[0]);
            Web2(dataGridView2, websties[1]);
            Web3(dataGridView3, websties[2]);
            Web4(dataGridView4, websties[3]);
        }
        private void Web1(DataGridView dgv,string website)
        {
            string url = website;
            string dataFilePath = Path.Combine(Application.StartupPath, "data", "1.txt");
            try
            {
                lblStatus.Text = "正在获取数据...";
                Application.DoEvents();

                // 创建WebClient并设置编码
                WebClient webClient = new WebClient();
                webClient.Encoding = System.Text.Encoding.UTF8;
                string htmlContent = webClient.DownloadString(url);

                // 加载HTML文档
                HtmlAgilityPack.HtmlDocument htmlDoc = new HtmlAgilityPack.HtmlDocument();
                htmlDoc.LoadHtml(htmlContent);

                // 获取class为artList的div元素
                HtmlNode artListDiv = htmlDoc.DocumentNode.SelectSingleNode("//div[@class='artList']");
                if (artListDiv == null)
                {
                    lblStatus.Text = "未找到class为artList的div元素";
                    return;
                }

                // 创建DataTable来存储数据
                DataTable dataTable = new DataTable();
                dataTable.Columns.Add("序号");
                dataTable.Columns.Add("标题");
                dataTable.Columns.Add("发布日期");
                dataTable.Columns.Add("链接");

                // 获取所有ul_art_row类的ul元素
                HtmlNodeCollection articleNodes = artListDiv.SelectNodes(".//ul[@class='ul_art_row']");
                if (articleNodes == null || articleNodes.Count == 0)
                {
                    lblStatus.Text = "未找到文章列表";
                    return;
                }

                // 读取已保存的数据
                string[] existingData = File.Exists(dataFilePath) ? File.ReadAllLines(dataFilePath) : new string[0];

                // 处理每篇文章
                int index = 1;
                foreach (HtmlNode articleNode in articleNodes)
                {
                    // 获取标题
                    HtmlNode titleNode = articleNode.SelectSingleNode(".//li[@class='li_art_title']/a");
                    string title = titleNode?.InnerText.Trim() ?? "无标题";
                    string link = titleNode?.GetAttributeValue("href", "") ?? "";

                    // 获取日期
                    HtmlNode dateNode = articleNode.SelectSingleNode(".//li[@class='li_art_date']");
                    string date = dateNode?.InnerText.Trim() ?? "无日期";

                    // 添加到DataTable
                    dataTable.Rows.Add(index++, title, date, link);
                }

                // 绑定到DataGridView
                dgv.DataSource = dataTable;
                lblStatus.Text = $"成功加载 {dataTable.Rows.Count} 篇文章";

                // 调整列宽
                dgv.Columns["序号"].Width = 60;
                dgv.Columns["标题"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
                dgv.Columns["发布日期"].Width = 100;
                dgv.Columns["链接"].Width = 200;

                // 标记新数据
                if (File.Exists(dataFilePath))
                {
                    MarkNewData(dataTable, existingData,dgv);
                }
            }
            catch (Exception ex)
            {
                lblStatus.Text = $"错误: {ex.Message}";
                MessageBox.Show($"发生错误:\n{ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void Web2(DataGridView dgv, string website)
        {
            string url = website;
            string dataFilePath = Path.Combine(Application.StartupPath, "data", "2.txt");

            try
            {
                lblStatus.Text = "正在获取数据...";
                Application.DoEvents();

                // 创建WebClient并设置编码
                WebClient webClient = new WebClient();
                webClient.Encoding = System.Text.Encoding.UTF8;
                string htmlContent = webClient.DownloadString(url);

                // 加载HTML文档
                HtmlAgilityPack.HtmlDocument htmlDoc = new HtmlAgilityPack.HtmlDocument();
                htmlDoc.LoadHtml(htmlContent);

                // 获取id为div_list的div元素
                HtmlNode listDiv = htmlDoc.DocumentNode.SelectSingleNode("//div[@id='div_list']");
                if (listDiv == null)
                {
                    lblStatus.Text = "未找到id为div_list的div元素";
                    return;
                }

                // 创建DataTable来存储数据
                DataTable dataTable = new DataTable();
                dataTable.Columns.Add("序号");
                dataTable.Columns.Add("标题");
                dataTable.Columns.Add("发布日期");
                dataTable.Columns.Add("链接");

                // 获取所有dl元素
                HtmlNodeCollection dlNodes = listDiv.SelectNodes(".//dl");
                if (dlNodes == null || dlNodes.Count == 0)
                {
                    lblStatus.Text = "未找到文章列表";
                    return;
                }

                // 读取已保存的数据（如果有）
                string[] existingData = File.Exists(dataFilePath) ? File.ReadAllLines(dataFilePath) : new string[0];

                // 处理每个dl元素
                int index = 1;
                foreach (HtmlNode dlNode in dlNodes)
                {
                    // 获取标题和链接
                    HtmlNode titleNode = dlNode.SelectSingleNode(".//dt/a");
                    string title = titleNode?.GetAttributeValue("title", "") ?? "无标题";
                    string displayText = titleNode?.InnerText.Trim() ?? "无标题";
                    string link = titleNode?.GetAttributeValue("href", "") ?? "";

                    // 获取日期
                    HtmlNode dateNode = dlNode.SelectSingleNode(".//dd");
                    string date = dateNode?.InnerText.Trim() ?? "无日期";

                    // 添加到DataTable
                    dataTable.Rows.Add(index++, displayText, date, link);
                }

                // 绑定到DataGridView
                dgv.DataSource = dataTable;
                lblStatus.Text = $"成功加载 {dataTable.Rows.Count} 条数据";

                // 调整列宽
                dgv.Columns["序号"].Width = 60;
                dgv.Columns["标题"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
                dgv.Columns["发布日期"].Width = 100;
                dgv.Columns["链接"].Width = 200;

                // 标记新数据（如果有）
                if (File.Exists(dataFilePath))
                {
                    MarkNewData(dataTable, existingData,dgv);
                }
            }
            catch (Exception ex)
            {
                lblStatus.Text = $"错误: {ex.Message}";
                MessageBox.Show($"发生错误:\n{ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        
        private void Web3(DataGridView dgv, string website)
        {
            string url = website;
            string dataFilePath = Path.Combine(Application.StartupPath, "data", "3.txt");

            try
            {
                lblStatus.Text = "正在获取数据...";
                Application.DoEvents();

                // 创建WebClient并设置编码
                WebClient webClient = new WebClient();
                webClient.Encoding = System.Text.Encoding.UTF8;
                string htmlContent = webClient.DownloadString(url);

                // 加载HTML文档
                HtmlAgilityPack.HtmlDocument htmlDoc = new HtmlAgilityPack.HtmlDocument();
                htmlDoc.LoadHtml(htmlContent);

                // 获取class为hakkaCulture-box-lsit的div元素
                HtmlNode listDiv = htmlDoc.DocumentNode.SelectSingleNode("//div[@class='hakkaCulture-box-lsit']");
                if (listDiv == null)
                {
                    lblStatus.Text = "未找到文章列表容器";
                    return;
                }

                // 创建DataTable来存储数据
                DataTable dataTable = new DataTable();
                dataTable.Columns.Add("序号");
                dataTable.Columns.Add("标题");
                dataTable.Columns.Add("发布日期");
                dataTable.Columns.Add("链接");

                // 获取所有p-list类的div元素
                HtmlNodeCollection articleNodes = listDiv.SelectNodes(".//div[@class='p-list']");
                if (articleNodes == null || articleNodes.Count == 0)
                {
                    lblStatus.Text = "未找到文章列表";
                    return;
                }

                // 读取已保存的数据（如果有）
                string[] existingData = File.Exists(dataFilePath) ? File.ReadAllLines(dataFilePath) : new string[0];

                // 处理每篇文章
                int index = 1;
                foreach (HtmlNode articleNode in articleNodes)
                {
                    // 获取标题和链接
                    HtmlNode titleNode = articleNode.SelectSingleNode(".//a[@class='p-list-content']");
                    string title = titleNode?.InnerText.Trim() ?? "无标题";
                    string link = titleNode?.GetAttributeValue("href", "") ?? "";

                    // 获取配置信息div
                    HtmlNode configureDiv = articleNode.SelectSingleNode(".//div[@class='p-list-configure clearfix']");

                    // 初始化日期和来源
                    string date = "无日期";

                    if (configureDiv != null)
                    {
                        // 获取发布日期（从p-list-left下的p-configure-date）
                        HtmlNode dateNode = configureDiv.SelectSingleNode(".//span[@class='p-configure-date ']");
                        if (dateNode != null)
                        {
                            string fullDateText = dateNode.InnerText.Trim();
                            date = fullDateText.Replace("发布日期：", "").Trim();
                        }

                    }

                    // 添加到DataTable
                    dataTable.Rows.Add(index++, title, date, link);
                }

                // 绑定到DataGridView
                dgv.DataSource = dataTable;
                lblStatus.Text = $"成功加载 {dataTable.Rows.Count} 条数据";

                // 调整列宽
                dgv.Columns["序号"].Width = 60;
                dgv.Columns["标题"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
                dgv.Columns["发布日期"].Width = 100;
                dgv.Columns["链接"].Width = 200;

                // 标记新数据（如果有）
                if (File.Exists(dataFilePath))
                {
                    MarkNewData(dataTable, existingData, dgv);
                }
            }
            catch (Exception ex)
            {
                lblStatus.Text = $"错误: {ex.Message}";
                MessageBox.Show($"发生错误:\n{ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void Web4(DataGridView dgv, string website)
        {
            string url = website;
            string dataFilePath = Path.Combine(Application.StartupPath, "data", "4.txt");

            try
            {
                lblStatus.Text = "正在获取数据...";
                Application.DoEvents();

                // 创建WebClient并设置编码
                WebClient webClient = new WebClient();
                webClient.Encoding = System.Text.Encoding.UTF8;
                string htmlContent = webClient.DownloadString(url);

                // 加载HTML文档
                HtmlAgilityPack.HtmlDocument htmlDoc = new HtmlAgilityPack.HtmlDocument();
                htmlDoc.LoadHtml(htmlContent);

                // 精确匹配class属性值
                HtmlNode listDiv = htmlDoc.DocumentNode.SelectSingleNode("//div[@class='list in-list-page']");
                if (listDiv == null)
                {
                    lblStatus.Text = "未找到文章列表容器";
                    MessageBox.Show("未找到class为'list in-list-page'的div元素", "错误",
                                  MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                // 创建DataTable
                DataTable dataTable = new DataTable();
                dataTable.Columns.Add("序号");
                dataTable.Columns.Add("标题");
                dataTable.Columns.Add("发布日期");
                dataTable.Columns.Add("链接");

                // 修改1：使用更宽松的dl元素选择器
                HtmlNodeCollection dlNodes = listDiv.SelectNodes(".//dl");
                if (dlNodes == null || dlNodes.Count == 0)
                {
                    // 修改2：尝试另一种选择器作为备选方案
                    dlNodes = listDiv.SelectNodes(".//div[contains(@class,'art-item')]");

                    if (dlNodes == null || dlNodes.Count == 0)
                    {
                        lblStatus.Text = "未找到文章列表";
                        MessageBox.Show("未找到任何文章条目(dl或div)", "错误",
                                      MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                }

                // 读取已保存的数据（如果有）
                string[] existingData = File.Exists(dataFilePath) ? File.ReadAllLines(dataFilePath) : new string[0];

                // 处理每个dl元素
                int index = 1;
                foreach (HtmlNode dlNode in dlNodes)
                {
                    try
                    {
                        // 修改3：使用更灵活的选择器获取标题和链接
                        HtmlNode titleNode = dlNode.SelectSingleNode(".//a[@title]") ??
                                            dlNode.SelectSingleNode(".//a[contains(@href,'content/post')]");

                        string title = titleNode?.InnerText.Trim() ?? "无标题";
                        string link = titleNode?.GetAttributeValue("href", "") ?? "";

                        // 修改4：使用更灵活的选择器获取发布日期
                        HtmlNode dateNode = dlNode.SelectSingleNode(".//dd") ??
                                           dlNode.SelectSingleNode(".//span[contains(@class,'date')]");

                        string date = dateNode?.InnerText.Trim() ?? "无日期";

                        // 添加到DataTable
                        dataTable.Rows.Add(index++, title, date, link);
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine($"处理第{index}条时出错: {ex.Message}");
                    }
                }

                if (dataTable.Rows.Count == 0)
                {
                    lblStatus.Text = "警告：找到条目但未提取到有效数据";
                    MessageBox.Show("解析到文章容器但未提取出有效数据", "警告",
                                  MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }

                // 绑定到DataGridView
                dgv.Invoke((MethodInvoker)delegate {
                    dgv.DataSource = dataTable;
                    dgv.Columns["序号"].Width = 60;
                    dgv.Columns["标题"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
                    dgv.Columns["发布日期"].Width = 100;
                    dgv.Columns["链接"].Width = 200;
                });

                lblStatus.Text = $"成功加载 {dataTable.Rows.Count} 条数据";

                // 标记新数据（如果有）
                if (File.Exists(dataFilePath))
                {
                    MarkNewData(dataTable, existingData,dgv);
                }
            }
            catch (WebException webEx)
            {
                lblStatus.Text = "网络错误";
                MessageBox.Show($"网络请求失败:\n{webEx.Message}", "网络错误",
                              MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (Exception ex)
            {
                lblStatus.Text = "系统错误";
                MessageBox.Show($"发生未预期错误:\n{ex.Message}", "系统错误",
                              MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void MarkNewData(DataTable dataTable, string[] existingData, DataGridView dgv)
        {
            foreach (DataGridViewRow row in dgv.Rows)
            {
                if (row.IsNewRow) continue;

                string title = row.Cells["标题"].Value.ToString();
                string date = row.Cells["发布日期"].Value.ToString();
                string link = row.Cells["链接"].Value.ToString();

                string rowData = $"{title}|{date}|{link}";

                bool existsInFile = false;
                foreach (string line in existingData)
                {
                    if (line.Contains(rowData))
                    {
                        existsInFile = true;
                        break;
                    }
                }

                if (!existsInFile)
                {
                    row.DefaultCellStyle.BackColor = Color.LightPink;
                }
            }
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            // 初始化DataGridView
            dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            dataGridView1.ReadOnly = true;
            dataGridView1.AllowUserToAddRows = false;
            dataGridView1.AllowUserToDeleteRows = false;
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            // 点击链接列时在浏览器中打开链接
            if (e.ColumnIndex == dataGridView1.Columns["链接"].Index && e.RowIndex >= 0)
            {
                string url = dataGridView1.Rows[e.RowIndex].Cells["链接"].Value.ToString();
                if (!string.IsNullOrEmpty(url))
                {
                    System.Diagnostics.Process.Start(url);
                }
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            SaveDgvData(dataGridView1,1);
            SaveDgvData(dataGridView2,2);
            SaveDgvData(dataGridView3,3);
            SaveDgvData(dataGridView4,4);
        }
        private void SaveDgvData(DataGridView dgv,int fileIndex)
        {
            try
            {
                if (dgv.Rows.Count == 0)
                {
                    MessageBox.Show("没有数据可保存", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                string dataFilePath = Path.Combine(Application.StartupPath, "data", fileIndex + ".txt");
                using (StreamWriter sw = new StreamWriter(dataFilePath, false, System.Text.Encoding.UTF8))
                {
                    foreach (DataGridViewRow row in dgv.Rows)
                    {
                        if (row.IsNewRow) continue;

                        string title = row.Cells["标题"].Value.ToString();
                        string date = row.Cells["发布日期"].Value.ToString();
                        string link = row.Cells["链接"].Value.ToString();

                        sw.WriteLine($"{title}|{date}|{link}");
                    }
                }

                MessageBox.Show("数据已成功保存", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
                lblStatus.Text = $"数据已保存到 {dataFilePath}";
            }
            catch (Exception ex)
            {
                MessageBox.Show($"保存数据时出错:\n{ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }
    }
}
